import BoxSource from "./BoxSource"
import clientEnum from "./client.enum"
import CylinderSource from "./CylinderSource"
import SphereSource from "./SphereSource"

export default class CreateSourceRoutine {
  userDataMap: Map<any, any>
  key: string
  argsDef: { argNames: string[]; varArg: boolean }
  updateSourceCall: any
  constructor(t) {
    this.userDataMap = new Map()
    this.key = "source.create"
    this.argsDef = { argNames: ["type", "options"], varArg: !1 }
    this.updateSourceCall = t.functionRegister.register("Sensor.updateSource", clientEnum.ht)
  }
  buildMessageData(t, e) {
    const s = e.options.hasOwnProperty("userData") ? e.options.userData : {}
    if (!(i = s) || "object" != typeof i) throw new Error("Error creating Source, userData was provided but not an object")
    var i
    this.userDataMap.set(t, Object.assign({}, s))
    const r = Object.assign({}, e.options)
    return delete r.userData, { type: e.type, options: r }
  }
  async onResponse(t, e) {
    const s = this.userDataMap.get(t) || {}
    return (
      this.userDataMap.delete(t),
      (function (t, e, s) {
        const { sourceId: i, type: r, volume: n } = t
        switch (r) {
          case clientEnum.ct.SPHERE:
            return new SphereSource(i, e, s, n)
          case clientEnum.ct.BOX:
            return new BoxSource(i, e, s, n)
          case clientEnum.ct.CYLINDER:
            return new CylinderSource(i, e, s, n)
        }
      })(e, this.updateSourceCall, s).facade
    )
  }
}
